//城市选择
var tcity = require("../../utils/citys.js");
var app = getApp()
var that
Page({
  data: {
    name:'',
    phone:'',
    provinces: [],
    province: "",
    citys: [],
    city: "",
    countys: [],
    county: '',
    value: [0, 0, 0],
    values: [0, 0, 0],
    condition: false,

    address_detail: "",  //详细地址
    adr_id:'',  //地址id
    ischecked:0,  //0是未设为默认地址
  },

  formSubmit: function (e) {
    var adds = e.detail.value;
    var cartId = this.data.cartId;
    // wx.request({
    //   url: app.d.ceshiUrl + '/Api/Address/add_adds',
    //   data: {
    //     user_id: app.d.userId,
    //     receiver: adds.name,
    //     tel: adds.phone,
    //     sheng: this.data.sheng,
    //     city: this.data.city,
    //     quyu: this.data.area,
    //     adds: adds.address,
    //     code: this.data.code,
    //   },
    //   method: 'POST', // OPTIONS, GET, HEAD, POST, PUT, DELETE, TRACE, CONNECT
    //   header: {// 设置请求的 header
    //     'Content-Type': 'application/x-www-form-urlencoded'
    //   },
    //   success: function (res) {
    //     // success
    //     var status = res.data.status;
    //     if (status == 1) {
    //       wx.showToast({
    //         title: '保存成功！',
    //         duration: 2000
    //       });
    //     } else {
    //       wx.showToast({
    //         title: res.data.err,
    //         duration: 2000
    //       });
    //     }
    //     wx.redirectTo({
    //       url: 'user-address/user-address?cartId=' + cartId
    //     });
    //   },
    //   fail: function () {
    //     // fail
    //     wx.showToast({
    //       title: '网络异常！',
    //       duration: 2000
    //     });
    //   }
    // })


  },
  onLoad: function (options) {
    that = this;
    tcity.init(that);
    var cityData = that.data.cityData;
    const provinces = [];
    const citys = [];
    const countys = [];
    for (let i = 0; i < cityData.length; i++) {
      provinces.push(cityData[i].name);
    }
    // console.log('省份完成');
    for (let i = 0; i < cityData[0].sub.length; i++) {
      citys.push(cityData[0].sub[i].name)
    }
    // console.log('city完成');
    for (let i = 0; i < cityData[0].sub[0].sub.length; i++) {
      countys.push(cityData[0].sub[0].sub[i].name)
    }
    that.setData({
      'provinces': provinces,
      'citys': citys,
      'countys': countys,
      'province': cityData[0].name,
      'city': cityData[0].sub[0].name,
      'county': cityData[0].sub[0].sub[0].name
    })
    
    if(options.id){  //如果有id则是编辑，没有则是添加新地址
      that.setData({ adr_id: options.id})
      that.get_adrr();
    }
  },
  up_adr:function(){
    if (that.data.name.trim() == '' || that.data.phone.trim() == '' || that.data.address_detail.trim() == ''){
      app.toast_up('数据不完整!')
      return;
    }
    var myreg = /^(1(([358][0-9])|[4][579]|[7][01235678]))\d{8}$/
    if (!myreg.test(that.data.phone.trim())){
      app.toast_up('手机号有误!')
      return;
    }
    app.req("updata_shop_adr.html", {
      "openid": app.data.openid, 
      "adr_id": that.data.adr_id, 
      " realname": that.data.name,
      "mobile": that.data.phone,
      " province": that.data.province,
     "city": that.data.city,
      "area": that.data.county,
      "address": that.data.address_detail,
      "isdefault":that.data.ischecked, }, function (res) {
      if (res.status == "success") {
        app.toast(res.mess)
        wx.navigateBack({ delta: 1 })  //返回上一级
      } else if (res.status == "error") {
        app.toast(res.mess)
      } else {
        app.modal("错误提示", "网络错误，是否重试？", function (re) {
          if (re) {
            that.up_adr()
          }
        })
      }
    })
  },
  bindChange: function (e) {
    //console.log(e);
    var val = e.detail.value
    var t = this.data.values;
    var cityData = this.data.cityData;
    if (val[0] != t[0]) {
      // console.log('province no ');
      const citys = [];
      const countys = [];

      for (let i = 0; i < cityData[val[0]].sub.length; i++) {
        citys.push(cityData[val[0]].sub[i].name)
      }
      for (let i = 0; i < cityData[val[0]].sub[0].sub.length; i++) {
        countys.push(cityData[val[0]].sub[0].sub[i].name)
      }

      this.setData({
        province: this.data.provinces[val[0]],
        city: cityData[val[0]].sub[0].name,
        citys: citys,
        county: cityData[val[0]].sub[0].sub[0].name,
        countys: countys,
        values: val,
        value: [val[0], 0, 0]
      })
      return;
    }
    if (val[1] != t[1]) {
      // console.log('city no');
      const countys = [];

      for (let i = 0; i < cityData[val[0]].sub[val[1]].sub.length; i++) {
        countys.push(cityData[val[0]].sub[val[1]].sub[i].name)
      }
      this.setData({
        city: this.data.citys[val[1]],
        county: cityData[val[0]].sub[val[1]].sub[0].name,
        countys: countys,
        values: val,
        value: [val[0], val[1], 0]
      })
      return;
    }
    if (val[2] != t[2]) {
      // console.log('county no');
      this.setData({
        county: this.data.countys[val[2]],
        values: val
      })
      return;
    }

  },
  open: function () {
    this.setData({
      condition: !this.data.condition
    })
  },
  adr_change:function(e){
    this.setData({
      address_detail: e.detail.value
    })
  },
  switchChange:function(e){
      // console.log(e)
      if (e.detail.value == false) {that.setData({ischecked:0})}
      else { that.setData({ ischecked: 1 }) }
  },
  Input_n:function(e){
    this.setData({
      name: e.detail.value
    })
  },
  Input_p: function (e) {
    this.setData({
      phone: e.detail.value
    })
  },

  get_adrr:function(){
    app.req("get_shop_one_adr.html", { "openid": app.data.openid, "adr_id": that.data.adr_id}, function (res) {
      if (res.status == "success") {
        that.setData({
          name: res.mess.realname,
          phone: res.mess.mobile,
          province: res.mess.province,
          city: res.mess.city,
          county: res.mess.area,
          address_detail: res.mess.address,
          ischecked: res.mess.isdefault,})
      } else if (res.status == "error") {
        app.toast(res.mess)
      } else {
        app.modal("错误提示", "网络错误，是否重试？", function (re) {
          if (re) {
            that.get_adrr()
          }
        })
      }
    })
  }
})